/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumxml;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives for Transformers.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Transformers {

    /**
     * No instance of this class.
     */
    private Transformers() {
    } // end empty constructor

    /**
     * Constructs a transformer factory.
     * @param ctxt context
     * @param unit ignored
     * @return a new transformer factory
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformerfactory_make(final CodeRunner ctxt,
                                                           final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(TransformerFactory.newInstance());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformerfactory_make(CodeRunner, Value)'

    /**
     * Sets the value of an attribute.
     * @param ctxt context
     * @param fact factory
     * @param name attribute name
     * @param value attribute value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformerfactory_set_attribute(final CodeRunner ctxt,
                                                                    final Value fact,
                                                                    final Value name,
                                                                    final Value value)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            f.setAttribute(name.asBlock().asString(), value.asBlock().asCustom());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformerfactory_set_attribute(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of an attribute.
     * @param ctxt context
     * @param fact factory
     * @param name attribute name
     * @return value of passed attribute
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformerfactory_get_attribute(final CodeRunner ctxt,
                                                                    final Value fact,
                                                                    final Value name)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.getAttribute(name.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformerfactory_get_attribute(CodeRunner, Value, Value)'

    /**
     * Sets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @param value feature value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformerfactory_set_feature(final CodeRunner ctxt,
                                                                  final Value fact,
                                                                  final Value name,
                                                                  final Value value)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            f.setFeature(name.asBlock().asString(), value == Value.TRUE);
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformerfactory_set_feature(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of a feature flag.
     * @param ctxt context
     * @param fact factory
     * @param name feature name
     * @return value of passed feature
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformerfactory_get_feature(final CodeRunner ctxt,
                                                                  final Value fact,
                                                                  final Value name)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            return f.getFeature(name.asBlock().asString())
                ? Value.TRUE
                : Value.FALSE;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformerfactory_get_feature(CodeRunner, Value, Value)'

    /**
     * Constructs an identity transformer.
     * @param ctxt context
     * @param fact factory
     * @return identity transformer
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_make_id(final CodeRunner ctxt,
                                                       final Value fact)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.newTransformer());
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_make_id(CodeRunner, Value)'

    /**
     * Constructs a transformer from a file.
     * @param ctxt context
     * @param fact factory
     * @param filename filename
     * @return transformer described by passed file
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_make_from_file(final CodeRunner ctxt,
                                                              final Value fact,
                                                              final Value filename)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            return Cadmium.createObject(f.newTransformer(new StreamSource(ctxt.getContext().getInputStreamForPath(filename))));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_make_from_file(CodeRunner, Value, Value)'

    /**
     * Constructs a transformer from a channel.
     * @param ctxt context
     * @param fact factory
     * @param channel channel
     * @return transformer described by passed channel
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_make_from_channel(final CodeRunner ctxt,
                                                                 final Value fact,
                                                                 final Value channel)
        throws Fail.Exception {
        try {
            final TransformerFactory f = (TransformerFactory) fact.asBlock().asCustom();
            final Channel ch = (Channel) channel.asBlock().asCustom();
            return Cadmium.createObject(f.newTransformer(new StreamSource(ch.asInputStream())));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_make_from_channel(CodeRunner, Value, Value)'

    /**
     * Clear the parameters of the passed transformer instance.
     * @param ctxt context
     * @param trans transformer instance
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_clear_parameters(final CodeRunner ctxt,
                                                                final Value trans)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            t.clearParameters();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_clear_parameters(CodeRunner, Value)'

    /**
     * Sets the value of a parameter.
     * @param ctxt context
     * @param trans transformer instance
     * @param name parameter name
     * @param value parameter value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_set_parameter(final CodeRunner ctxt,
                                                             final Value trans,
                                                             final Value name,
                                                             final Value value)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            t.setParameter(name.asBlock().asString(), value.asBlock().asCustom());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_set_parameter(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of a parameter.
     * @param ctxt context
     * @param trans transformer instance
     * @param name parameter name
     * @return value of passed parameter
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_get_parameter(final CodeRunner ctxt,
                                                             final Value trans,
                                                             final Value name)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            return Cadmium.createObject(t.getParameter(name.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_get_parameter(CodeRunner, Value, Value)'

    /**
     * Sets the value of a property.
     * @param ctxt context
     * @param trans transformer instance
     * @param name property name
     * @param value property value
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_set_output_property(final CodeRunner ctxt,
                                                                   final Value trans,
                                                                   final Value name,
                                                                   final Value value)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            t.setOutputProperty(name.asBlock().asString(), value.asBlock().asString());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_set_output_property(CodeRunner, Value, Value, Value)'

    /**
     * Gets the value of a property.
     * @param ctxt context
     * @param trans transformer instance
     * @param name property name
     * @return value of passed property
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_get_output_property(final CodeRunner ctxt,
                                                                   final Value trans,
                                                                   final Value name)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            final String s = t.getOutputProperty(name.asBlock().asString());
            return Value.createFromBlock(Block.createString(s));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_get_output_property(CodeRunner, Value, Value)'

    /**
     * Applies a transformer.
     * @param ctxt context
     * @param trans transformer instance to apply
     * @param src source document (file)
     * @param dst destination document (file)
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_transform_file_to_file(final CodeRunner ctxt,
                                                                      final Value trans,
                                                                      final Value src,
                                                                      final Value dst)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            t.transform(new StreamSource(ctxt.getContext().getInputStreamForPath(src)),
                        new StreamResult(ctxt.getContext().getOutputStreamForPath(dst)));
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_transform_file_to_file(CodeRunner, Value, Value, Value)'

    /**
     * Applies a transformer.
     * @param ctxt context
     * @param trans transformer instance to apply
     * @param src source document (file)
     * @param dst destination document (channel)
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_transform_file_to_channel(final CodeRunner ctxt,
                                                                         final Value trans,
                                                                         final Value src,
                                                                         final Value dst)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            final Channel ch = (Channel) dst.asBlock().asCustom();
            t.transform(new StreamSource(ctxt.getContext().getInputStreamForPath(src)),
                        new StreamResult(ch.asOutputStream()));
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_transform_file_to_channel(CodeRunner, Value, Value, Value)'

    /**
     * Applies a transformer.
     * @param ctxt context
     * @param trans transformer instance to apply
     * @param src source document (channel)
     * @param dst destination document (file)
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_transform_channel_to_file(final CodeRunner ctxt,
                                                                         final Value trans,
                                                                         final Value src,
                                                                         final Value dst)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            final Channel ch = (Channel) src.asBlock().asCustom();
            t.transform(new StreamSource(ch.asInputStream()),
                        new StreamResult(ctxt.getContext().getOutputStreamForPath(dst)));
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_transform_channel_to_file(CodeRunner, Value, Value, Value)'

    /**
     * Applies a transformer.
     * @param ctxt context
     * @param trans transformer instance to apply
     * @param src source document (channel)
     * @param dst destination document (channel)
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_transform_channel_to_channel(final CodeRunner ctxt,
                                                                            final Value trans,
                                                                            final Value src,
                                                                            final Value dst)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            final Channel ch1 = (Channel) src.asBlock().asCustom();
            final Channel ch2 = (Channel) dst.asBlock().asCustom();
            t.transform(new StreamSource(ch1.asInputStream()),
                        new StreamResult(ch2.asOutputStream()));
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_transform_channel_to_channel(CodeRunner, Value, Value, Value)'

    /**
     * Resets the passed transformer instance.
     * @param ctxt context
     * @param trans transformer instance
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumxml_transformer_reset(final CodeRunner ctxt,
                                                     final Value trans)
        throws Fail.Exception {
        try {
            final Transformer t = (Transformer) trans.asBlock().asCustom();
            t.reset();
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumxml_transformer_reset(CodeRunner, Value)'

} // end class 'Transformers'
